package kr.hkit.powerjava.chapter15.menu;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class InputEmp extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	
	private JButton btnAdd;
	private JPanel panel;
	private JLabel lblNum;
	private JTextField txtNum;
	private JLabel lblName;
	private JTextField txtName;
	private JLabel lblTitle;
	private JComboBox<String> txtTitle;
	private JLabel lblMgn;
	private JTextField txtMgn;
	private JLabel lblSalary;
	private JTextField txtSalary;
	private JLabel lblDno;
	private JComboBox txtDno;

	private Connection con;
	private EmployeeDAO dao;
	private HashMap<String, Integer> list;
	
	public InputEmp() {
		con = ConnectionFactory.getConnection();
		dao = new EmployeeDAO(con);
		initialize();
		
	}
	
	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		btnAdd = new JButton("추가");
		btnAdd.addActionListener(this);
		add(btnAdd, BorderLayout.SOUTH);
		
		panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(new GridLayout(0, 2, 0, 5));
		
		lblNum = new JLabel("사원번호");
		lblNum.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblNum);
		
		txtNum = new JTextField();
		panel.add(txtNum);
		txtNum.setColumns(10);
		
		lblName = new JLabel("사원명");
		lblName.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblName);
		
		txtName = new JTextField();
		panel.add(txtName);
		txtName.setColumns(10);
		
		lblTitle = new JLabel("직책");
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblTitle);
		
		/*	array 이용
		String[] list = {"사원","대리","과장","부장","사장"};
		txtTitle = new JComboBox<String>(list);*/
		/*arraylist 이용*/
		try {
			DefaultComboBoxModel<String> model = dao.getTitles();
			txtTitle = new JComboBox<String>(model);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		panel.add(txtTitle);
		
		lblMgn = new JLabel("상급자");
		lblMgn.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblMgn);
		
		txtMgn = new JTextField();
		panel.add(txtMgn);
		txtMgn.setColumns(10);
		
		lblSalary = new JLabel("급여");
		lblSalary.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblSalary);
		
		txtSalary = new JTextField();
		panel.add(txtSalary);
		txtSalary.setColumns(10);
		
		lblDno = new JLabel("부서");
		lblDno.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblDno);
		
		/*	array 이용 
		String[] list = {"영업",	"기획", "	테스팅", "개발", "총무"};
		txtDno = new JComboBox(list);
		*/

		list = new HashMap<String, Integer>();
		list.put("영업", 1);
		list.put("기획",2);
		list.put("테스팅", 5);
		list.put("개발", 6);
		list.put("총무", 7);
		Set<String> arr= list.keySet();
		txtDno = new JComboBox<>(arr.toArray());
		panel.add(txtDno);
	}
	
	
	public String getLblNewLabel() {
		return lblNum.getText();
	}
	
	public void setLblNewLabel(String title) {
		this.lblNum.setText(title);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAdd) {
			actionPerformedBtnAdd(e);
		}
	}
	
	private boolean inValidCheck(){
		//공백반 체크
		if (txtNum.getText().equals("")) {
			txtNum.requestFocus();
			return false;
		}
		if (txtName.getText().equals("")){
			txtName.requestFocus();
			return false;
		}
		return true;
	}
	
	private Employee getInfo(){
		if (inValidCheck()){ 
			int empNo = Integer.parseInt(txtNum.getText().trim());
			String empName = txtName.getText().trim();
			String title = (String) txtTitle.getSelectedItem();
			int manager = Integer.parseInt(txtMgn.getText().trim());
			int salary = Integer.parseInt(txtSalary.getText().trim());
//			int dno = Integer.parseInt(txtDno.getText().trim());
			int dno = list.get(txtDno.getSelectedItem());
			return new Employee(empNo, empName, title, manager, salary, dno);
		}else{
			return null;
		}
		
	}
	
	protected void actionPerformedBtnAdd(ActionEvent e) {
		try{
			JOptionPane.showMessageDialog(null, getInfo().toString());
		}catch(NullPointerException e1){
			JOptionPane.showMessageDialog(null, "잘못된 정보");
		}
	}
}










